package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import business.BusinessImpl;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.Color;

import javax.swing.AbstractAction;
import javax.swing.Action;

import constant.Constant;
import dto.DTOContact;

import java.awt.event.KeyEvent;

public class Login extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtVisa;
	private JLabel lblwrongvisa;
	private BusinessImpl businessImpl;
	private static Login frame;
	private final Action actok = new actOk();
	private final Action actcancel = new actCancel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new Login();
					frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {

		// charge the interface
		businessImpl = new BusinessImpl();
		this.setModal(true);
		this.setTitle("Connexion");
		setBounds(100, 100, 204, 130);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		lblwrongvisa = new JLabel("Veuillez entrer un visa autoris\u00E9");
		lblwrongvisa.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblwrongvisa.setForeground(Color.RED);
		lblwrongvisa.setVisible(false);
		contentPane.add(lblwrongvisa, BorderLayout.NORTH);

		JButton btnOk = new JButton("Ok");
		btnOk.setMnemonic('O');
		btnOk.setAction(actok);
		btnOk.setMaximumSize(new Dimension(90, 23));
		btnOk.setMinimumSize(new Dimension(90, 23));
		panel.add(btnOk);

		JButton btnCancel = new JButton("Annuler");
		btnCancel.setMnemonic('A');
		btnCancel.setAction(actcancel);
		btnCancel.setMaximumSize(new Dimension(85, 23));
		btnCancel.setMinimumSize(new Dimension(85, 23));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		panel.add(btnCancel);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

		JLabel lblVisa = new JLabel("Visa :");
		lblVisa.setFont(new Font("Tahoma", Font.PLAIN, 15));
		panel_1.add(lblVisa);

		txtVisa = new JTextField();
		txtVisa.setMaximumSize(new Dimension(2147483647, 20));
		panel_1.add(txtVisa);
		txtVisa.setColumns(10);

		// set the default button on enter
		JRootPane rootPane = SwingUtilities.getRootPane(btnOk);
		rootPane.setDefaultButton(btnOk);

	}

	private class actOk extends AbstractAction {
		// Action acting when the user press the ok button
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public actOk() {
			putValue(MNEMONIC_KEY, KeyEvent.VK_O);
			putValue(NAME, "Ok");
			putValue(SHORT_DESCRIPTION, "Valider");
		}

		public void actionPerformed(ActionEvent e) {

			// check if the contact exist
			DTOContact contact = businessImpl.getContactByVisa(txtVisa
					.getText());

			if (contact != null) {

				// check if the contact is not a student
				if (!(contact.getTypeOfContact() == Constant
						.typeofcontact("STUDENT"))) {
					// launch the main class
					Main main = new Main(businessImpl.getContactByVisa(txtVisa
							.getText()));
					frame.dispose();
					main.setVisible(true);

				} else
					lblwrongvisa.setVisible(true);
			} else {
				lblwrongvisa.setVisible(true);
			}
		}
	}

	private class actCancel extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public actCancel() {
			putValue(MNEMONIC_KEY, KeyEvent.VK_A);
			putValue(NAME, "Annuler");
			putValue(SHORT_DESCRIPTION, "Annuler");
		}

		public void actionPerformed(ActionEvent e) {
			frame.dispose();

		}
	}
}
